import java.sql.SQLException;
import java.util.ArrayList;

/* 
 * 	Notes.java - This class enables the user (employee) to add new notes and reminders.
 * 
 * 	@Author : BilCIS
 */


public class Notes 
{
	// Variables declaration
	private ArrayList<String> notes;
	private ArrayList<String> topic;
	private String str;
	
	private Mysql mysql;
	private Employee emp;
	// End of variables declaration
	
	// conscturcotr
	public Notes( Employee emp) throws SQLException
	{	
		this.emp = emp;
		mysql = new Mysql(emp.getID());
		str = mysql.getNotes();
		notes = new ArrayList<String>();
		topic = new ArrayList<String>();
		seperateNotes();
		seperateTopics();
	}
	
	// updates the values of notes through employee to database
	public void update() throws SQLException
	{
		emp.setNotes(str);
	}
	
	// adds new notes
	public void addNote( String topic, String note )
	{
		str = str + topic + "--" + note + "**";
		
		try {
			update();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		seperateTopics();
		seperateNotes();
	}
	
	// gets num of notes
	public int getNumOfNotes()
	{
		return notes.size();
	}
	
	// gets the topic
	public String getTopic( int i )
	{
		if ( notes.size() > i && i >= 0)
			return topic.get( i );
		return null;
	}
	
	// gets the note specific
	public String getNote( int i )
	{
		if ( notes.size() > i && i >= 0)
			return notes.get( i );
		return null;
	}
	
	public String getFirstNote()
	{
		return notes.get( notes.size() - 1);
	}
	
	public String getFirstTopic()
	{
		return topic.get( topic.size() - 1);
	}
	
	// gets all topics
	public String getTopics()
	{
		String tpc = topic.get(topic.size()-1);
		for ( int i = topic.size() - 2; i >= 0 ; i--)
		{
			tpc = tpc + "\n" + topic.get(i);
		}
				
		return tpc;
	}
	
	// removes notes
	public void removeNote( int i )
	{
		notes.remove(i);
		topic.remove(i);
		updateStr();
		try {
			update();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	// updates
	public void updateStr()
	{
		String newStr = "**";
		
		for ( int i = 0; i < notes.size(); i++)
		{
			newStr = newStr + topic.get(i) + "--" + notes.get(i) + "**";
		}
		str = newStr;
		seperateTopics();
		seperateNotes();
	}
	
	// seperate notes
	public void seperateNotes()
	{
		notes.removeAll(notes);
		String clone = str;
		clone = clone.substring( clone.indexOf("**") + 2 );
		while ( clone.indexOf("**") != -1 )
		{
			notes.add(clone.substring( clone.indexOf("--") + 2, clone.indexOf("**") ));
			clone = clone.substring( clone.indexOf("**") + 2 );
		}
	}
	
	// seperate topics
	public void seperateTopics()
	{
		topic.removeAll(topic);
		String clone = str;
		while ( clone.indexOf("--") != -1 )
		{
			topic.add(clone.substring( clone.indexOf("**") + 2, clone.indexOf("--") ));
			clone = clone.substring( clone.indexOf("--") + 2 );
		}
	}
}
